package com.xinqi.modules.course.clazz.convert;

import com.fasterxml.jackson.databind.JsonNode;
import com.xinqi.common.base.utils.json.JSON;
import com.xinqi.common.mapstruct.CommonConvertConfig;
import com.xinqi.modules.course.clazz.domain.ClassEntity;
import com.xinqi.modules.course.clazz.dto.req.ClassCountLimitUpdateDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassCreateDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassUpdateDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassResultDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassResultNewDTO;
import com.xinqi.modules.course.course.enums.ClassStatusEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import java.util.List;

/**
 * 班级表转换
 *
 * @author: system
 * @date: 2023/01/17
 */
@Mapper(config = CommonConvertConfig.class)
public abstract class ClassConvert {

    @Mapping(target = "auditInfo", ignore = true)
    public abstract ClassEntity create(ClassCreateDTO s);

    public abstract ClassCreateDTO create(ClassEntity c);

    @Mapping(target = "auditInfo", ignore = true)
    public abstract ClassEntity update(ClassUpdateDTO s, @MappingTarget ClassEntity t);

    @Mapping(target = "auditInfo", ignore = true)
    public abstract ClassEntity update(ClassUpdateDTO s);

    @Mapping(target = "auditInfo", ignore = true)
    public abstract ClassResultDTO convert(ClassEntity s);

    public abstract List<ClassResultNewDTO> convert(List<ClassResultDTO> list);

    public abstract ClassCreateDTO convert(ClassUpdateDTO c);

    public abstract ClassCountLimitUpdateDTO createTo(Long classId);

    public abstract ClassEntity newentity(ClassEntity c);

    @AfterMapping
    public void after(ClassCreateDTO s, @MappingTarget ClassEntity t) {
        t.setStatus(ClassStatusEnum.NORMAL);
        if (CollectionUtils.isNotEmpty(s.getAuditInfo())) {
            t.setAuditInfo(JSON.stringify(s.getAuditInfo()));
        }
    }

    @AfterMapping
    public void after(ClassUpdateDTO s, @MappingTarget ClassEntity t) {
        if (CollectionUtils.isNotEmpty(s.getAuditInfo())) {
            t.setAuditInfo(JSON.stringify(s.getAuditInfo()));
        }
    }

    @AfterMapping
    public void after(@MappingTarget ClassResultDTO t, ClassEntity s) {
        if (StringUtils.isNotEmpty(s.getAuditInfo())) {
            t.setAuditInfo(JSON.parse(s.getAuditInfo(), JsonNode.class));
        }
    }

}
